﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;
using System.IO;
using System.Xml;
namespace BUS
{
    public class AccountBUS
    {
        AccountDAO dao;
        public AccountBUS()
        {
            dao = new AccountDAO();
        }
        /// <summary>
        /// Kiểm tra 1 email
        /// </summary>
        /// <param name="email">email cần kiểm tra</param>
        /// <returns>true nếu tồn tại, false nếu chưa tồn tại</returns>
        public bool CheckEmail(string email)
        {
            return dao.EmailExists(email);
        }
        /// <summary>
        /// Tạo mới tài khoản, gửi đối tượng cho DAO
        /// </summary>
        /// <param name="account"></param>
        /// <returns>1 nếu thành công, 0 nếu thất bại</returns>
        public int CreateNewAccount(AccountDTO account)
        {
            return dao.InsertAccount(account);
        }
        /// <summary>
        /// Cập nhật tài khoản
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public bool UpdateAccountInfo(AccountDTO account)
        {
            return dao.UpdateAccount(account);
        }

        /// <summary>
        /// Lấy 1 account
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public AccountDTO GetAccountInfo(string email)
        {
            return dao.GetAccountInfo(email);
        }

        /// <summary>
        /// Kiểm tra password của nsd ứng mới email có đúng hay ko
        /// gọi hàm verifyHash để kiểm tra
        /// </summary>
        /// <param name="email">email của người dùng</param>
        /// <param name="password">password cần kiểm tra</param>
        /// <returns>true nếu đúng pass, false nếu pass sai</returns>
        public bool CheckPassword(string email, string password)
        {
            OurHash hash = new OurHash();
            AccountDTO account = GetAccountInfo(email);
            string strHash = account.Hash;
            return hash.VerifyHash(password, "", strHash);

        }

        /// <summary>
        /// viết 1 string xuống 1 file
        /// </summary>
        /// <param name="inputFile">đường dẫn của file</param>
        /// <param name="Key">Key</param>
        private void WriteKey(string inputFile, string Key)
        {
            FileStream fs = new FileStream(inputFile, FileMode.Create, FileAccess.Write);
            byte[] keyBytes = Convert.FromBase64String(Key);
            fs.Write(keyBytes, 0, keyBytes.Length);
            fs.Close();
        }

        /// <summary>
        /// Export thông tin của account sang 1 file xml
        /// </summary>
        /// <param name="filePath"> đường dẫn</param>
        /// <param name="account"> thông tin tài khoản</param>
        /// <returns>true nếu thành công, false nếu ghi file thất bại</returns>
        public bool ExportToXML(string filePath, AccountDTO account)
        {
            bool success = false;
            try
            {
                XmlTextWriter xmlwriter = new XmlTextWriter(filePath, Encoding.UTF8);
                xmlwriter.WriteStartDocument();

                xmlwriter.WriteStartElement("AccountInfo");

                xmlwriter.WriteStartElement("Email");
                xmlwriter.WriteString(account.Email);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("Name");
                xmlwriter.WriteString(account.Name);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("Hash");
                xmlwriter.WriteValue(account.Hash);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("Salt");
                xmlwriter.WriteValue(account.Salt);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("DayOfBirth");
                xmlwriter.WriteValue(account.DayOfBirth);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("Address");
                xmlwriter.WriteValue(account.Address);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("Phone");
                xmlwriter.WriteValue(account.Phone);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("PublicKey");
                xmlwriter.WriteValue(account.PublicKey);
                xmlwriter.WriteEndElement();

                xmlwriter.WriteStartElement("PrivateKey");
                xmlwriter.WriteValue(account.PrivateKey);
                xmlwriter.WriteEndElement();



                xmlwriter.WriteEndElement();
                xmlwriter.Close();
                success = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return success;
        }

        /// <summary>
        /// Import thông tin 1 acoount từ 1 file
        /// </summary>
        /// <param name="filePath"> đường dẫn tới file</param>
        /// <returns></returns>
        public bool ImportFromXML(string filePath)
        {
            // string filePath = "test.xml";
            bool success = false;
            try
            {
                AccountDTO account = new AccountDTO();
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                XmlNode node = doc.DocumentElement;
                account.Email = node.ChildNodes[0].InnerText;
                account.Name = node.ChildNodes[1].InnerText;
                account.Hash = node.ChildNodes[2].InnerText;
                account.Salt = node.ChildNodes[3].InnerText;
                account.DayOfBirth = DateTime.Parse(node.ChildNodes[4].InnerText);
                account.Address = node.ChildNodes[5].InnerText;
                account.Phone = node.ChildNodes[6].InnerText;
                account.PublicKey = node.ChildNodes[7].InnerText;
                account.PrivateKey = node.ChildNodes[8].InnerText;

                /*               XmlTextReader reader = new XmlTextReader(new FileStream(filePath, FileMode.Open));

                                reader.ReadStartElement("AccountInfo");

                                reader.ReadToFollowing("Email");
                                account.Email=reader.ReadElementContentAsString();
                                reader.ReadToFollowing("Name");
                                account.Name = reader.ReadElementContentAsString();
                                reader.ReadToFollowing("Hash");
                                account.Hash = reader.ReadElementContentAsString();
                                reader.ReadToFollowing("Salt");
                                account.Salt = reader.ReadElementContentAsString();
                                reader.ReadToFollowing("DayOfBirth");
                                account.DayOfBirth = reader.ReadElementContentAsDateTime();
                                reader.ReadToFollowing("Address");
                                account.Address = reader.ReadElementContentAsString();
                                reader.ReadToFollowing("Phone");
                                account.Phone = reader.ReadElementContentAsString();
                                reader.ReadToFollowing("PublicKey");
                                account.PublicKey = reader.ReadElementContentAsString();
                                reader.ReadToFollowing("PrivateKey");
                                account.PrivateKey = reader.ReadElementContentAsString();

                                reader.ReadEndElement();
                                reader.Close();
                 */


                if (CreateNewAccount(account) != 0)
                    success = true;

            }
            catch (Exception)
            {

                throw;
            }
            return success;
        }

        public List<AccountDTO> GetAllAccountInfo()
        {
            List<AccountDTO> list = new List<AccountDTO>();
            AccountDAO dao = new AccountDAO();
            list = dao.GetAllAccountInfo();
            return list;
        }

    }
}
